/**
 * 
 */
package com.gragra.sampling.binary;
import com.gragra.util.BooleanInterweave;
import it.unimi.dsi.fastutil.longs.Long2DoubleAVLTreeMap;
/**
 * @author Christoph Teichmann
 * 16.09.2013
 */
public class PairCounter
{
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{return "PairCounter [count=" + this.count + "]";}
	/**
	 * 
	 */
	private final Long2DoubleAVLTreeMap count = new Long2DoubleAVLTreeMap();
	/**
	 * 
	 */
	public void clear()
	{count.clear();}
	/**
	 * 
	 * @param pos
	 * @param i
	 * @param d
	 */
	public void set(int start, int end, double d)
	{
		long full = BooleanInterweave.combine(start, end);
		handleVal(full, d);
	}
	/**
	 * 
	 * @param start
	 * @param middle
	 * @return
	 */
	public double get(int start, int end)
	{return this.count.get(BooleanInterweave.combine(start, end));}
	/**
	 * 
	 * @param from
	 * @param to
	 * @param by
	 */
	public void add(int from, int to, double by)
	{
		long full = BooleanInterweave.combine(from, to);
		double val = this.count.get(full);
		val += by;
		handleVal(full, val);
	}
	/**
	 * @param full
	 * @param val
	 */
	private void handleVal(long full, double val)
	{
		if(val == 0.0)
		{this.count.remove(full);}
		else
		{this.count.put(full, val);}
	}
}